package vip.zhonghui.www.skills

import android.annotation.SuppressLint
import android.app.DatePickerDialog
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.widget.*
import java.util.*
import kotlin.collections.ArrayList

class Activityhome : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_activityhome)
        val sp = getSharedPreferences("local", MODE_PRIVATE)
        val ed = sp.edit()
        val from = findViewById<EditText>(R.id.from)
        val to = findViewById<EditText>(R.id.to)
        val date = findViewById<TextView>(R.id.date)
        val search = findViewById<Button>(R.id.search)
        val img = findViewById<ImageView>(R.id.img)

        val handle = Handler()
        val imgs = ArrayList<Int>()
        imgs.add(R.mipmap.banner)
        imgs.add(R.mipmap.banner2)
        imgs.add(R.mipmap.banner3)
        var i = 0
        val runnable = object :Runnable{
            override fun run() {
                if (i >= imgs.size){
                    i = 0
                }
                runOnUiThread {
                    img.setImageResource(imgs[i])
                    handle.postDelayed(this,2000)
                }
            }

        }
        handle.postDelayed(runnable,0)


        from.setText(sp.getString("from",""))
        to.setText(sp.getString("to",""))


        date.setOnClickListener {
            val calendar = Calendar.getInstance()
            val datepicker = DatePickerDialog(
                this,
                myDatePicker(),
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH),
            )
            datepicker.show()
        }

        search.setOnClickListener {
            if (from.text.toString()!="" && to.text.toString()!="" && date.text.toString()!=""){
                ed.putString("from",from.text.toString()).apply()
                ed.putString("to",to.text.toString()).apply()
                startActivity(Intent(this,ActivityList::class.java))
            }else{
                Toast.makeText(this, "请补充完整信息", Toast.LENGTH_SHORT).show()
            }
        }





    }
    inner class myDatePicker:DatePickerDialog.OnDateSetListener{
        @SuppressLint("SetTextI18n")
        override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
            runOnUiThread {
                findViewById<TextView>(R.id.date).text = ("${year}年${month}月${dayOfMonth}日")

                val sp = getSharedPreferences("local", MODE_PRIVATE)
                val ed = sp.edit()

                ed.putString("year",year.toString()).apply()
                ed.putString("month",month.toString()).apply()
                ed.putString("dayOfMonth",dayOfMonth.toString()).apply()

            }
        }

    }

}